package com.ebt.m.proposal_v2.ui;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ProgressBar;

import com.ebt.m.AppContext;
import com.ebt.m.R;
import com.ebt.m.commons.utils.ActivityUtils;
import com.ebt.m.data.bean.Brand;
import com.ebt.m.proposal_v2.bean.temp.ActivityFinishStack;
import com.ebt.m.proposal_v2.dao.request.ParamBrandProducts;
import com.ebt.m.proposal_v2.dao.request.ParamGetRisks;
import com.ebt.m.proposal_v2.mvp.base.MVPCompatActivity;
import com.ebt.m.proposal_v2.mvp.contract.SearchProductContract;
import com.ebt.m.proposal_v2.mvp.presenter.SearchProductPresenter;
import com.ebt.m.proposal_v2.ui.view.SpecProductSearchListView;
import com.ebt.m.utils.DataValidation;
import com.ebt.m.utils.UIHelper;
import com.ebt.m.view.ClearEditText;
import com.ebt.m.wiki.SpecProductFilterHistory;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 主险搜索
 *
 * @author archer.qi
 *         Created on 2017/5/26.
 */
public class SearchProductsActivity extends MVPCompatActivity<SearchProductPresenter> implements SearchProductContract.View {
    public static final String ARG_PARAM = "Param";

    @BindView(R.id.btn_cancel)
    FrameLayout btnCancel;
    @BindView(R.id.btn_search)
    FrameLayout btnSearch;
    @BindView(R.id.search_view)
    ClearEditText searchView;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.content)
    FrameLayout content;
    @BindView(R.id.progress_bar)
    ProgressBar mProgressBar;

    private SpecProductFilterHistory specProductSearchHistories;
    private SpecProductSearchListView specProductSearchListView;

    private ParamGetRisks mArg;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    }

    @Override
    public void initArguments() {
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            mArg = (ParamGetRisks) extras.getSerializable(ARG_PARAM);
        }
    }

    @Override
    public void setEmptyView(boolean isEmpty) {

    }

    @Override
    protected SearchProductPresenter createPresenter() {
        return new SearchProductPresenter(getApplicationContext(), this);
    }

    @Override
    protected int setContentResource() {
        return R.layout.activity_search_products;
    }

    @Override
    protected void onActivityCreate() {
        initArguments();
        initHistories();
        ActivityFinishStack.getInstance().add(this);
    }

    @Override
    protected void onActivityDestroy() {
        ActivityFinishStack.getInstance().remove(this);
    }

    private void initHistories() {
        content.removeAllViews();
        specProductSearchHistories = new SpecProductFilterHistory(this);
        content.addView(specProductSearchHistories);
        specProductSearchHistories.update(4);
        specProductSearchHistories.setOnHistoryItemClick(new SpecProductFilterHistory.OnHistoryItemClick() {
            @Override
            public void onItemClick(Brand brand, int position) {
                if (brand == null) return;
                searchView.setText(brand.getBrandShortName());
                btnSearch.performClick();
            }
        });
    }

    private void search() {
        String keyword = searchView.getText().toString().trim();
        if (DataValidation.isEmpty(keyword)) {
            UIHelper.makeToast(this, "请输入搜索关键字！");
            return;
        }
        specProductSearchListView = new SpecProductSearchListView(this, mArg);
        specProductSearchListView.update(generateSelectionParams());
        content.removeAllViews();
        content.addView(specProductSearchListView);
        ActivityUtils.closeInput(this);
    }

    private ParamBrandProducts generateSelectionParams() {
        ParamBrandProducts param = new ParamBrandProducts();
        param.companyId = AppContext.getCurrentUser().getCompanyInfo().getCompanyID();
        param.keyword = searchView.getText().toString().trim();
        if (mArg != null) {
            param.age = mArg.age;
            param.sex = mArg.sex;
            param.profession = mArg.profession;
        }
        return param;
    }

    @OnClick({R.id.btn_cancel, R.id.btn_search, R.id.search_view})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_cancel:
                finish();
                break;
            case R.id.btn_search:
                search();
                break;
        }
    }

}
